package com.mobi.cacti;

import java.io.ByteArrayInputStream;
import java.io.InputStream;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.mobi.cacti.common.Utils;

import com.mobi.cacti.rabbit.controllers.LoginActivityController;
import com.mobi.cacti.rabbit.controllers.LoginActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;
import com.mobi.cacti.rabbit.response.UserInfo;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;

public class LoginActivity extends Activity implements LoginActivityControllerCallback {

	final LoginActivityController loginActivityController = LoginActivityController.getInstance();
	private EditText edtUsername, edtPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		loginActivityController.delegate = this;
		
		super.onCreate(savedInstanceState);
		/*
		setContentView(R.layout.atv_login);	

		edtUsername = (EditText) findViewById(R.id.edtUsername);
		edtPassword = (EditText) findViewById(R.id.edtPassword);

		//test
		//edtUsername.setText("su");//"");
		//edtPassword.setText("sugraph");//"eF8fgmRH");
		edtUsername.setText("bu");//"");
		edtPassword.setText("bugraph");
		
		Button btnSignIn = (Button) findViewById(R.id.btnSignIn);
		btnSignIn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
///				Handle login button click here
				loginActivityController.authenticateWithServer(edtUsername.getText().toString(), edtPassword.getText().toString());
				Button btnSignIn = (Button) findViewById(R.id.btnSignIn);
				btnSignIn.setEnabled(false);
//				//*** OFFLINE MODE *******************************************
									String response = "";
				
									String pathFile = Utils.getRootDirectory("0001", "test")+"ReqMobileLogin.xml";
									response = Utils.loadDataFromFile(pathFile);//testt load xml from file
									if(response == null || response.equals("")){
										//Utils.showDialogMsgInThread("Path: " +pathFile, "Not found file xml for test", LoginActivity.this);
										Utils.showDialogMsgInThread("","Login failed!", LoginActivity.this);
										if(Utils.pd != null)Utils.pd.dismiss();
										return;
									}
																		
									//login success..
									Cache.userInfo = new UserInfo();
									Cache.userInfo.UserName = edtUsername.getText().toString();
									Cache.userInfo.Password = edtPassword.getText().toString();
				
									InputStream is = new ByteArrayInputStream(response.getBytes());
									ReqMobileLogin obj = SAXXMLParser.reqMobileLogin(is);
									Cache.reqMobileLogin = obj;
									Log.i("tai", "reqMobileLogin = "+Cache.reqMobileLogin.Code_Response);
				
									Intent i = new Intent(LoginActivity.this,MainActivity.class);
									startActivity(i);
									Cache.menuClick=1;
			}
		});
		*/
	}


	@Override
	public void onDestroy()
	{
		super.onDestroy();			
		if(Utils.pd != null)Utils.pd.dismiss();
	}


	@Override
	public void loginActivityControllerCompleteAuthenticationToServer() {
		
		if(Cache.reqMobileLogin == null ||!Cache.reqMobileLogin.isLoginSuccess())
		{
			Log.i("tai", "LOGIN BAD");
			
			if(Cache.reqMobileLogin == null)
				Log.i("tai", "NULLL");
			
			//Button btnSignIn = (Button) findViewById(R.id.btnSignIn);
			//btnSignIn.setEnabled(true);
		}
		else
		{
			Log.i("tai", "LOGIN GOOD");
			Intent i = new Intent(LoginActivity.this,MainActivity.class);
			startActivity(i);
			Cache.menuClick=1;
		}
		
		
	}

}
